package com.wz.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wz.entity.Dep;
import com.wz.entity.Emp;
import com.wz.service.DepService;
import com.wz.service.impl.DepServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("dep")
public class DepController {
    @Autowired
    private DepServiceImpl depService;

   @RequestMapping("adddep")
   public String toadd(){
       return "adddap";
   }
    private PageInfo<Dep> getPageInfo( ){
        PageHelper.startPage(1, 4);
        List<Dep> deps = depService.findDep();
        PageInfo<Dep> pageInfo = new PageInfo<>(deps);//使用PageInfo包装类
        return pageInfo;

    }

    @RequestMapping("/delete/{deptno}")
    public String delte(@PathVariable("deptno") String deptno,Model model,HttpServletRequest request) {
        depService.delete(deptno);
        System.out.println("delete方法执行成功！！");
        request.getSession().setAttribute("pageInfo1",getPageInfo());
        return "showdep";
    }

    @PostMapping("addDep")
    public String addDep(Dep dep,HttpServletRequest request) {
        depService.save(dep);
        request.getSession().setAttribute("pageInfo1",getPageInfo());
        return "showdep";
    }
}
